package cz.boxdesign.library.entity.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.stereotype.Component;

import cz.boxdesign.library.entity.Author;
import cz.boxdesign.library.entity.dao.query.AuthorSqlQuery;

@Component(value = "autorDao")
public class AutorDaoImpl extends EntityDaoAbstract<Author, AuthorSqlQuery> {

  /**
   * @param result
   * @param rs
   * @throws SQLException
   */
  @Override
  void getEntityList(List<Author> result, ResultSet rs) throws SQLException {
    while (rs.next()) {
      result.add(new Author(rs.getInt(1), rs.getString(2), rs.getString(3)));
    }
  }

  /**
   * @param entity
   * @param clause
   * @param ps
   * @throws SQLException
   */
  @Override
  void fillStatement(Author entity, int queryValue, PreparedStatement ps) throws SQLException {
    AuthorSqlQuery query = AuthorSqlQuery.getByValue(queryValue);
    switch (query) {
      case UPDATE: {
        ps.setLong(3, entity.getId());
      }
      case INSERT: {
        ps.setString(1, entity.getJmeno());
        ps.setString(2, entity.getPrijmeni());
        break;
      }
      case DELETE: {
        ps.setLong(1, entity.getId());
        break;
      }
      default:
        throw new IllegalArgumentException();
    }
  }

  @Override
  String getSqlQuery(int queryValue) {
    return AuthorSqlQuery.getByValue(queryValue).getQuery();
  }

}
